package com.zhongshan.ga.mobile.logic;

import java.util.ArrayList;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.commonvar.CommendConfig;
import com.zhongshan.ga.commonvar.PHPWebUrl;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mobile.tools.JsonResultUtils;
import com.zhongshan.ga.mybatis.bean.ActivityInfo;
import com.zhongshan.ga.mybatis.dao.IActivityInfoDao;
import com.zhongshan.ga.utils.MyBatisUtil;

/**
 * 客户端活动列表逻辑处理类
 * 
 * @author Tom
 * 
 */

public class GetActivityInfoListLogic extends ParentLogic {

	/**
	 * 用于处理客户端请求获取活动信息
	 * 
	 * @param representation
	 *            客户端请求体
	 * @return JsonObject
	 */
	public JsonObject getActivityInfoList(Representation representation) {
		JsonObject resultJson = new JsonObject();
		try {
			// 获取请求中的json
			String requestText = representation.getText();
			JsonObject acceptJson = new JsonParser().parse(requestText)
					.getAsJsonObject();

			// 对数据做初步检查
			resultJson = basicCheck(acceptJson,
					CommendConfig.ACTIVITY_INFO_LIST, false);
			if (resultJson.get(ArgsConfig.RESULT_CODE).getAsString()
					.equals(ArgsConfig.RESPONSE_SUCCESS)) {
				// 调用接口 获取数据
				SqlSession session = MyBatisUtil.getSqlSession(true);
				IActivityInfoDao mapper = session
						.getMapper(IActivityInfoDao.class);

				// 获取条数
				JsonObject dataJson = acceptJson.get(ArgsConfig.REQUEST_DATA)
						.getAsJsonObject();
				Map<String, Integer> activityInfoMap = jsonIndexLimitMap(dataJson);

				// 调用接口返回数据, 并检查数据
				ArrayList<ActivityInfo> activityInfoList = mapper
						.getAllActivityInfo(activityInfoMap);
				if (activityInfoList != null) {
					for (ActivityInfo item : activityInfoList) {
						item.setActivityCover(PHPWebUrl.ACTIVITY_COVER_IMAGE_URL
								+ item.getActivityCover());
					}

					// 获取数据 把数据放入json结果集中
					String dataJsonStr = new Gson().toJson(activityInfoList);
					JsonObject tempJson = new JsonObject();
					tempJson.add(ArgsConfig.RESULT_DATA, new JsonParser()
							.parse(dataJsonStr).getAsJsonArray());
					resultJson.add(ArgsConfig.REQUEST_DATA,
							tempJson.getAsJsonObject());
				} else {
					resultJson.addProperty(ArgsConfig.RESULT_CODE,
							ArgsConfig.RESPONSE_FAIL);
					resultJson.addProperty(ArgsConfig.RESULT_DESC, "查询出错");
				}
				session.close();

				GenerateLog
						.outDebug("*************getActivityInfoList 查询结果****************************");
				GenerateLog.outDebug(resultJson.toString());

			} else {
				GenerateLog
						.outDebug("*************getActivityInfoList 验证失败****************************");
			}

		} catch (Exception e) {
			e.printStackTrace();
			GenerateLog
					.outDebug("*************getActivityInfoList 请求数据取出出错****************************");
			resultJson = JsonResultUtils.createResultJson(
					CommendConfig.ACTIVITY_INFO_LIST, "请求数据出错", false);
		}

		return resultJson;

	}

}
